package com.right.health.app.net;

import com.magics.http.model.HttpParams;
import com.right.health.app.bean.AllergySourceResponse;
import com.right.health.app.bean.FamilayHistoryResponse;
import com.right.health.app.bean.MarriageResponse;
import com.right.health.app.bean.PastResponse;
import com.right.health.app.bean.SurgeryResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/*
 * Created by hopeliao on 2019-12-01
 *
 */
public class SickHistoryApi extends IBaseApi {

    public void getSurgerys(String memberId, int pageNo) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberId);

            PageUtil.addPageJson(jsonObject, pageNo);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_SURGERY_LIST, params, SurgeryResponse.class);
    }

    //添加手术
    public void addSurgery(String userId, String name, String time, String desc) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", userId);
            jsonObject.put("operationname", name);
            jsonObject.put("operationtime", time);
            jsonObject.put("operationdesc", desc);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_ADDSURGERY, params, null);
    }

    public void addFamilyHistory(String memberid, String name, String type, String content) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);
            jsonObject.put("diseasename", name);
            jsonObject.put("diseasetype", type);
            jsonObject.put("diseasedesc", content);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_ADD_FAMILY_HISTORY, params, null);
    }

    public void getFamilyHistoryList(String memberid, int pageNo) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);

            PageUtil.addPageJson(jsonObject, pageNo);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_LIST_FAMILY_HISTORY, params, FamilayHistoryResponse.class);
    }

    public void getMarriageHistory(String memberid) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_GET_MARRIAGE, params, MarriageResponse.class);
    }

    public void updateMarriage(String memberid, String maritaltype, String reproductivetype) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);
            jsonObject.put("maritaltype", maritaltype);
            jsonObject.put("reproductivetype", reproductivetype);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_UPDATE_MARRIAGE, params, null);
    }

    public void getPast(String memberid) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_GET_PAST, params, PastResponse.class);
    }

    public void updatePast(String memberid, String desc) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);
            jsonObject.put("pasthistorydesc", desc);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_UPDATE_PAST, params, null);
    }


    public void getAlleraySouce(String memberid) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_GETALLERGY_SOURCE, params, AllergySourceResponse.class);
    }

    public void updateAlleraySouce(String memberid, List<AllergySourceResponse.AllergySourceInfo> data) {
        HttpParams params = new HttpParams();

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);

            JSONArray array = new JSONArray();
            for(AllergySourceResponse.AllergySourceInfo info:data) {
                if("1".equals(info.anaphylactogenstatus)){
                    array.put(info.anaphylactogenid);
                }
            }
            jsonObject.put("anaphylactogenids", array);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_GETALLERGY_UPDATE_SOURCE, params, null);
    }

    public void addAlleraySource(String memberid, String name) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberid", memberid);
            jsonObject.put("anaphylactogenname", name);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_ADD_UPDATE_SOURCE, params, AllergySourceResponse.class);
    }
}
